package ics.zychen;

import ics.zychen.auth.RenrenOAuth;
import ics.zychen.auth.SinaOAuthConstant;

import java.util.ArrayList;
import java.util.HashMap;

import com.renren.api.connect.android.Renren;

import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.http.RequestToken;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class SnsSelectActivity extends Activity {
	public static final int SINA = 0;
	public static final int RENREN = 1;
	
	
	private void authorize(int what){
			switch(what){
			case SINA:
				System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
		    	System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
		    	Weibo weibo = new Weibo();
            	RequestToken requestToken;
				try {
					requestToken =weibo.getOAuthRequestToken("weibo4android://SinaOAuth");
					SinaOAuthConstant.getInstance().setRequestToken(requestToken);
					Uri uri = Uri.parse(requestToken.getAuthenticationURL()+ "&display=mobile");
					startActivity(new Intent(Intent.ACTION_VIEW, uri));
				} catch (WeiboException e) {
					e.printStackTrace();
				}
				break;
			case RENREN:
				/*final String API_KEY = "f79cfacaa4e943349fd23458b2fe3d5d";
				final String SECRET_KEY = "3a640ad285cc4f4fbd5ddc06640537be";
				final String APP_ID = "197463";
				
				Renren renren = new Renren(API_KEY, SECRET_KEY, APP_ID, SnsSelectActivity.this);
				Bundle bundle = new Bundle();
				bundle.putParcelable("RENREN", renren);
				*/
				Intent intent = new Intent();
				intent.setClass(this, RenrenOAuth.class);
				startActivity(intent);
				break;
			}
	}
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sns_list);
		
		ListView list = (ListView) findViewById(R.id.list_sns);
		ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String,Object>>();
		HashMap<String, Object> sina = new HashMap<String, Object>();
		sina.put("SnsIcon", R.drawable.icon_sina);
		sina.put("SnsName", "新浪微博");
		listItem.add(sina);
		HashMap<String, Object> renren = new HashMap<String, Object>();
		renren.put("SnsIcon", R.drawable.icon_renren);
		renren.put("SnsName", "人人网");
		listItem.add(renren);
		
		SimpleAdapter snsListAdapter = new SimpleAdapter(this, listItem, R.layout.sns_list_item, 
				new String[]{"SnsIcon","SnsName"}, new int[]{R.id.sns_icon, R.id.sns_name});
		list.setAdapter(snsListAdapter);
		
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if(0 == arg2){ //select sina weibo
					authorize(SINA);
				} else if(1 == arg2){ //select renren
					authorize(RENREN);
				}
			}
		});
	}
}
